#!/usr/bin/env python
# -*- coding: utf-8 -*-
from flask import Flask
from flask.ext.bootstrap import Bootstrap
from flask.ext.moment import Moment
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.mail import Mail
from config import config
from flask.ext.login import LoginManager
from flask.ext.pagedown import PageDown

bootstrap = Bootstrap()
mail = Mail()
moment = Moment()
db = SQLAlchemy()
pagedown = PageDown()

login_manger = LoginManager()
login_manger.session_protection = 'strong'
login_manger.login_view = 'auth.login'



def create_app(config_name):
    app = Flask(__name__)
    app.config.from_object(config[config_name])
    config[config_name].init_app(app)

    bootstrap.init_app(app)
    mail.init_app(app)
    moment.init_app(app)
    db.init_app(app)
    login_manger.init_app(app)
    pagedown.init_app(app)

    from .application import application as app_blueprint
    app.register_blueprint(app_blueprint, url_prefix='/app')
    from .auth import auth as auth_blueprint
    app.register_blueprint(auth_blueprint, url_prefix='/auth')
    #导入api的蓝图, 在程序启动的时候创建
    from .hosts import hosts as hosts_blueprint
    # 注册api的蓝图
    app.register_blueprint(hosts_blueprint, url_prefix='/hosts')

    return app
